package com.orangelife.mobile.propertyOld.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.propertyOld.adapter.PropertyBillListOldAdapter;
import com.orangelife.mobile.propertyOld.biz.PropertyHttpOldBiz;
import com.orangelife.mobile.util.BLog;
import com.orangelife.mobile.widget.ScrollListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 旧的物业缴费列表（旧版没有全选功能）
 * Created by shz on 2015/12/3.Modify by wgf on 160601.
 *
 * @author shz
 */
public class PropertyBillListOldActivity extends OrangeLifeBaseActivity {
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvTip)
    TextView tvTip;
    @Bind(R.id.llPropertyBill)
    RelativeLayout llPropertyBill;
    @Bind(R.id.lvPropertyBillList)
    ScrollListView lvPropertyBillList;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    /**
     * 从服务器获取的我的物业缴费的信息主体
     */
    private Map<String, Object> mResponseMap = new HashMap<>();
    /**
     * 我的物业缴费信息
     */
    private List<Map<String, Object>> mList = new ArrayList<>();
    private String mTip;
    private PropertyBillListOldAdapter mAdapter;
    private String commID;
    private String apartmentID;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_property_bill_list_old);
        ButterKnife.bind(this);
        getIntentInfo();
        initView();
        requestBillList();
        setAdapter();
        setListener();
    }

    private void getIntentInfo() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            commID = bundle.getString("commID");
            apartmentID = bundle.getString("apartmentID");
        }
    }

    private void initView() {
        tvTitle.setText("缴费账单");
    }

    private void setListener() {
        //注意是setOnLayoutClickListener()
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestBillList();
            }
        });
    }

    /**
     * 设置界面上的值
     */
    private void setTip() {
        tvTip.setText(mTip);
    }

    /**
     * 设置适配器
     */
    private void setAdapter() {
        mAdapter = new PropertyBillListOldAdapter(PropertyBillListOldActivity.this, mList);
        lvPropertyBillList.setAdapter(mAdapter);
    }

    /**
     * 获取我的物业缴费信息
     */
    private void requestBillList() {
        PropertyHttpOldBiz.getLastOrder(commID, apartmentID, new VolleyHttpUtil.ResponseCallBack() {
                    @Override
                    public void handleResponse(JSONObject response, int errCode) {
                        emptyLayout.dismiss();
                        mResponseMap = PropertyHttpOldBiz.handleLastOrder(response);
                        if (mResponseMap != null) {
                            mTip = String.valueOf(mResponseMap.get("tip"));
                            mList = (List<Map<String, Object>>) mResponseMap.get("pageEntity");
                            if(mList==null||mList.size()==0){
                                emptyLayout.setErrorType(EmptyLayout.NODATA);
                            }else{
                                setTip();
                                mAdapter.setList(mList);
                                mAdapter.notifyDataSetChanged();
                            }
                        }else{
                            emptyLayout.setErrorMessage("获取数据失败");
                            emptyLayout.setErrorType(EmptyLayout.NODATA);
                        }

                    }
                }

        );
    }

    @Override
    protected void onResume() {
        super.onResume();
        BLog.getInstance().intAppmod(BLog.PMFEEORDER);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BLog.getInstance().outAppmod(BLog.PMFEEORDER);
    }
}
